/*
	HUMANIZED MESSAGES 1.1
	idea - http://www.humanized.com/weblog/2006/09/11/monolog_boxes_and_transparent_messages
	jQuery version - http://humanmsg.googlecode.com
	
	New in 1.1
	* Prototype v1.6.0 support
	* Scriptaculous v1.8.2 support
	* Extra small bump at end of vertical shake
	* Removed race condition of trying to bounce tab while already bouncing
*/

Effect.ShakeVertical = function(element) {
	if(humanMsg.bounce)
		return false;
	humanMsg.bounce = true;
	element = $(element);
	var oldStyle = {
		top: Element.getStyle(element, 'top'),
		left: Element.getStyle(element, 'left') };
	return new Effect.Move(element, 
		{ x:  0, y: -40, duration: 0.1, afterFinishInternal: function(effect) {
		new Effect.Move(effect.element,
		{ x: 0, y: 40, duration: 0.08, afterFinishInternal: function(effect) {
		new Effect.Move(effect.element,
		{ x:  0, y: -5, duration: 0.08, afterFinishInternal: function(effect) {
		new Effect.Move(effect.element,
		{ x: 0, y: 5, duration: 0.08, afterFinishInternal: function(effect) { with(Element) {
		undoPositioned(effect.element);
		setStyle(effect.element, oldStyle);
		humanMsg.bounce = false;
	}}}) }}) }}) }});
}

var humanMsg = {
	setup: function(appendTo, logName, msgOpacity) {
		humanMsg.msgID = 'humanMsg';
		humanMsg.logID = 'humanMsgLog';
		humanMsg.listID = 'humanMsgList';
		humanMsg.slideSpeed = 0.5;
		humanMsg.bounce = false;

		// appendTo is the element the msg is appended to
		if (appendTo == undefined)
			appendTo = 'body';

		// The text on the Log tab
		if (logName == undefined)
			logName = 'Message Log';

		// Opacity of the message
		humanMsg.msgOpacity = .8;

		if (msgOpacity != undefined) 
			humanMsg.msgOpacity = parseFloat(msgOpacity);

		// Inject the message structure
		$$(appendTo)[0].insert('<div id="'+humanMsg.msgID+'" class="humanMsg" style="display:none;"><div class="round"></div><p></p><div class="round"></div></div> <div id="'+humanMsg.logID+'"><p id="logTab">'+logName+'</p><ul id="'+humanMsg.listID+'" style="display:none;"></ul></div>');
		
		Event.observe('logTab', 'click', humanMsg.toggleLog.bind(humanMsg));
	},
	toggleLog: function(e){
		new Effect.toggle(humanMsg.listID, 'blind', {duration: humanMsg.slideSpeed, queue:'end'});
	},
	toggleMsg: function(){
		new Effect.Appear(humanMsg.msgID, {duration:0.25,queue:'end'});
	},
	displayMsg: function(msg) {
		if (msg == '')
			return;

		clearTimeout(humanMsg.t2);
		
		// Inject message
		$(humanMsg.msgID).down('p').update(msg);
		setTimeout("humanMsg.toggleMsg();", 100);
		// Show message
		var listEl = $(humanMsg.listID);
		listEl.insert({top:'<li style="display:none;">'+msg+'</li>'});
		if(!listEl.visible()){
			listEl.down('li').show();
			new Effect.ShakeVertical('logTab', {duration:1,queue:'end'});
		}
		else
			new Effect.BlindDown(listEl.down('li'), {duration:0.25,queue:'end'});

		// Watch for mouse & keyboard in .5s
		humanMsg.t1 = setTimeout("humanMsg.bindEvents()", 700)
		// Remove message after 5s
		humanMsg.t2 = setTimeout("humanMsg.removeMsg()", 5000)
	},

	bindEvents: function() {
	// Remove message if mouse is moved or key is pressed
		Event.observe(window, 'mousemove', humanMsg.removeMsg);
		Event.observe(window, 'click', humanMsg.removeMsg);
		Event.observe(window, 'keypress', humanMsg.removeMsg);
	},

	removeMsg: function() {
		// Unbind mouse & keyboard
		Event.stopObserving(window, 'mousemove', humanMsg.removeMsg);
		Event.stopObserving(window, 'click', humanMsg.removeMsg);
		Event.stopObserving(window, 'keypress', humanMsg.removeMsg);

		// If message is fully transparent, fade it out
		if($(humanMsg.msgID).getStyle('opacity') == humanMsg.msgOpacity)
			new Effect.Fade(humanMsg.msgID, {duration:0.25,queue:'end'});
	}
};

Event.observe(window, 'load', function() {
	humanMsg.setup();
})